package com.douyang.controller;


import cn.dev33.satoken.oauth2.processor.SaOAuth2ServerProcessor;
import cn.dev33.satoken.stp.StpUtil;
import com.douyang.VO.AuthClientVO;
import com.douyang.base.model.Result;
import com.douyang.base.model.UserBaseInfo;
import com.douyang.base.utils.IdWorkerUtils;
import com.douyang.dto.OauthLoginDTO;
import com.douyang.pojo.AuthClient;
import com.douyang.service.AuthClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/oauth2")
public class Oauth2Controller {

    @Autowired
    private AuthClientService authClientService;

    @PostMapping("/doLogin")
    public Result<String> doLogin(@RequestBody OauthLoginDTO oauthLoginDTO) {
        UserBaseInfo userBaseInfo = authClientService.doLogin(oauthLoginDTO);
        StpUtil.login(userBaseInfo.getClientId());
        return Result.success(StpUtil.getTokenName()+" "+StpUtil.getTokenValue());
    }
    @GetMapping("/authorize")
    public Result<String> authorize() {
        return authClientService.authorize();
    }

    @GetMapping("/getAppInfo")
    public Result<AuthClientVO> getAppInfo(String hostName) {
        return Result.success(authClientService.getAppInfo(hostName));
    }


    @GetMapping("/getTokenByCode")
    public Result<String> getToken(String code) {
        return Result.success(authClientService.getTokenByCode(code));
    }

    @GetMapping("/getUserInfoByToken")
    public Result<UserBaseInfo> getUserInfoByToken(String token) {
        return Result.success(authClientService.getUserInfoByToken(token));
    }

    @GetMapping("/checkLogin")
    public Result<String> checkLogin() {
        StpUtil.checkLogin();
        return Result.success("ok");
    }
}
